package com.example.queryexpressdelivery_01.activity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;
import androidx.recyclerview.widget.DividerItemDecoration;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.example.queryexpressdelivery_01.common.DeliveryDAL;
import com.example.queryexpressdelivery_01.entity.FeedbackData;
import com.example.queryexpressdelivery_01.R;
import com.example.queryexpressdelivery_01.adapter.UserAdapter;
import com.example.queryexpressdelivery_01.entity.UserData;
import com.google.gson.Gson;

import java.util.List;

public class AdminActivity extends AppCompatActivity implements UserAdapter.OnItemClickListener {

    private TextView tvAdminTitle;
    private RecyclerView rvAdminData;
    private ImageView ivAdminLogOut;
    private List<UserData> userList;
    private DeliveryDAL deliveryDAL = new DeliveryDAL(this);
    private Gson gson = new Gson();
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_admin);
        ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main), (v, insets) -> {
            Insets systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars());
            v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom);
            return insets;
        });
        initView();
        userList = deliveryDAL.queryUserAll();
        UserData user = new UserData(0,"用户","电话","密码","邮箱","未处理信息","上一次登录时间","状态");
        userList.add(0,user);
        if (userList.size()>1){
            for (int i=1;i<userList.size();i++){
                FeedbackData feedbackData = gson.fromJson(userList.get(i).getData(),FeedbackData.class);
                int size = 0;
                if (feedbackData!=null){
                    if (feedbackData.getProblemfeedback()!=null){
                        for (int j=0;j<feedbackData.getProblemfeedback().size();j++){
                            if (feedbackData.getProblemfeedback().get(j).getRecover()==null)size++;
                        }
                    }
                }
                userList.get(i).setData(String.valueOf(size));
            }
        }
        rvAdminData.setLayoutManager(new LinearLayoutManager(this));
        UserAdapter userAdapter = new UserAdapter(userList);
        rvAdminData.addItemDecoration(new DividerItemDecoration(this,DividerItemDecoration.VERTICAL));
        userAdapter.setOnItemClickListener(this);
        rvAdminData.setAdapter(userAdapter);

        ivAdminLogOut.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(AdminActivity.this, LoginActivity.class);
                startActivity(intent);
                finish();
            }
        });
    }
    public void onItemClick(UserData user) {

        Intent intent = new Intent(AdminActivity.this, UserDetailsActivity.class);
        intent.putExtra("id", user.getId());
        // 启动新的 Activity
        startActivity(intent);
        finish();
    }
    private void initView() {
        tvAdminTitle = (TextView) findViewById(R.id.tvAdminTitle);
        rvAdminData = (RecyclerView) findViewById(R.id.rvAdminData);
        ivAdminLogOut = (ImageView) findViewById(R.id.ivAdminLogOut);
    }
}